package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

public interface OrdersMapper {
    void insert(Orders order);

    /**
     * 根据订单号和用户id查询订单
     *
     * @param orderNumber
     * @param userId
     */
    @Select("select * from orders where number = #{orderNumber} and user_id= #{userId}")
    Orders getByNumberAndUserId(String orderNumber, Long userId);

    /**
     * 修改订单信息
     *
     * @param orders
     */
    void update(Orders orders);

    @Select("select * from orders where status = #{status} and order_time < #{time}")
    List<Orders> selectTimeOutOrder(Integer status, LocalDateTime time);

    Page<OrderVO> getPage(OrdersPageQueryDTO dto);

    @Select("SELECT SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) AS toBeConfirmed,SUM(CASE WHEN status = 3 THEN 1 ELSE 0 END) AS confirmed,SUM(CASE WHEN status = 4 THEN 1 ELSE 0 END) AS deliveryInProgress FROM orders")
    OrderStatisticsVO statistics();

    @Select("select * from orders where id = #{id}")
    OrderVO getById(Long id);

    @Select("select sum(amount) from orders where status = #{status} and order_time between #{begin} and #{end}")
    Double getByStatusAndDay(Map map);

    @Select("select count(*) as total,count(case when status = 5 then 1 end ) as valid from orders where order_time between #{begin} and #{end}")
    Map getTotalAndValidOrderCount(LocalDateTime begin, LocalDateTime end);

    @MapKey("name")
    List<Map> top10(Map requestMap);
}
